package com.imti.mall.admin.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.imti.mall.admin.service.UmsAdminService;
import com.imti.mall.admin.service.UmsRoleService;
import com.imti.mall.common.Api.CommonResult;
import com.imti.mall.feign.clients.OrderFeignClient;
import com.imti.mall.model.admin.query.UmsAdminLoginParam;
import com.imti.mall.model.admin.pojo.UmsAdmin;
import com.imti.mall.model.admin.pojo.UmsRole;
import com.imti.mall.model.order.vo.OmsOrderDetail;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@Api(tags = "后台用户管理")
@RequestMapping("/api/v1/admin")
public class UserAdminController {

    @Autowired
    private UmsAdminService adminService;
    @Autowired
    private UmsRoleService umsRoleService;
    @Autowired
    private OrderFeignClient orderFeignClient;


    @ApiOperation(value = "管理员登录，成功返回token")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public CommonResult login(@RequestBody UmsAdminLoginParam umsAdminLoginParam) {

        if (ObjectUtil.isNull(umsAdminLoginParam)) {
            return CommonResult.failed();
        }
        return adminService.login(umsAdminLoginParam);
    }

    @ApiOperation(value = "获取当前登录用户信息")
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public CommonResult getAdminInfo() {
        UmsAdmin umsAdmin = adminService.getCurrentAdmin();
        Map<String, Object> data = new HashMap<>();
        data.put("username", umsAdmin.getUsername());
        data.put("menus", umsRoleService.getMenuList(umsAdmin.getId()));
        data.put("icon", umsAdmin.getIcon());
        List<UmsRole> roleList = adminService.getRoleList(umsAdmin.getId());
        if(CollUtil.isNotEmpty(roleList)){
            List<String> roles = roleList.stream().map(UmsRole::getName).collect(Collectors.toList());
            data.put("roles",roles);
        }
        return CommonResult.success(data);
    }

    @ApiOperation(value = "远程调用测试，调用订单详情")
    @GetMapping("/detail/{orderId}")
    public CommonResult<OmsOrderDetail> getOrderDetail(@PathVariable Long orderId){
       return orderFeignClient.detail(orderId);
    }


}
